package com.study.zzedu.fragment.tab.onlinetest;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.adapter.ExercisesAdapter;
import com.study.zzedu.base.BaseFragment;
import com.study.zzedu.base.BaseView;
import com.study.zzedu.entity.ExercisesItem;
import com.study.zzedu.ui.errorset.ErrorSetActivity;
import com.study.zzedu.utils.AppConstant;
import com.study.zzedu.utils.LogUtil;
import com.study.zzedu.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by kiros on 2020/9/5.
 * 在线测试页面
 */

public class OnLineTestFragment extends BaseFragment< OnlineTestContract.IView, OnLineTestPresenter> implements View.OnClickListener, OnlineTestContract.IView{

    private TextView tv_wrong_count;
    private RecyclerView rl_exercises;
    private RecyclerView.LayoutManager layoutManager;
    private ExercisesAdapter exercisesAdapter;
    private List<ExercisesItem> exercisesItems = new ArrayList<>();
    private RelativeLayout rl_wrong_topic;
    @Override
    protected OnLineTestPresenter createPresenter() {
        return new OnLineTestPresenter();
    }

    @Override
    protected  OnlineTestContract.IView createView() {
        return this;
    }

    @Override
    protected int getLayoutID() {
        return R.layout.fragment_test;
    }

    @Override
    protected void initView(View view) {
        rl_exercises = view.findViewById(R.id.rl_exercises);
        rl_wrong_topic = view.findViewById(R.id.rl_wrong_topic);
        tv_wrong_count = view.findViewById(R.id.tv_wrong_count);
    }

    @Override
    protected void initData() {
        layoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        LogUtil.d("aaaaaa"," OnLineTestFragment ----- onStart");

        updata();
        exercisesAdapter = new ExercisesAdapter(OnLineTestFragment.this, exercisesItems);
        rl_exercises.setLayoutManager(layoutManager);
        rl_exercises.setAdapter(exercisesAdapter);
    }

    private void updata(){
        getPresenter().getChapterInfo(getContext(), AppConstant.courseCode);
        getPresenter().queryErrorSubjectCount(getContext(), AppConstant.courseCode);
    }
    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    protected void initListener() {
        rl_wrong_topic.setOnClickListener(this);
    }

    @Override
    public void getData(String data) {
    }

    @Override
    public void showError(String error) {
        if (null != error && !TextUtils.isEmpty(error)) {
            ToastUtil.showShortToast(error);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_wrong_topic:
                Intent intent = new Intent(getContext(), ErrorSetActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void OnQueryErrorSubjectCount(String data) {
        try {
            JSONObject jObject = new JSONObject(data);

            JSONArray jArray = jObject.getJSONArray("data");
            if(!(jArray.length()>0)){
                //ToastUtil.showShortToast("在线测试无数据");
                return;
            }else {
                JSONObject job = jArray.getJSONObject(0);
                Iterator<String> sets =  job.keys();

                while (sets.hasNext()){
                    String value = job.getString(sets.next());
                    tv_wrong_count.setText(value);
                    return;
                }
                tv_wrong_count.setText("0");
                return;
            }

        } catch (Exception e) {
            e.toString();
        }
    }

    @Override
    public void OnQueryCourseChapters(String data) {
        try {
            exercisesItems.clear();
            JSONObject jObject = new JSONObject(data);

            JSONArray jArray = jObject.getJSONArray("data");
            for (int i = 0;i<jArray.length();i++){
                exercisesItems.add(new ExercisesItem(
                        jArray.getJSONObject(i).getString("chapterCode"),
                        jArray.getJSONObject(i).getInt("hasTestTimes"),
                        (float) jArray.getJSONObject(i).getDouble("curScore"),
                        (float) jArray.getJSONObject(i).getDouble("totalScore"),
                        jArray.getJSONObject(i).getString("name"),
                        jArray.getJSONObject(i).getString("courseCode")
                ));
            }
            if(!(jArray.length()>0)){
                //ToastUtil.showShortToast("在线测试无数据");
                return;
            }
            getActivity().findViewById(R.id.rl_default_test).setVisibility(View.GONE);

            exercisesAdapter.notifyDataSetChanged();

        } catch (Exception e) {
            e.toString();
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == AppConstant.RequestCode_OnLineToErrorSet){
            updata();
            //ToastUtil.showShortToast("正在更新数据。。。");
        }
    }
}
